#pragma once

#include <pthread.h>

class Mutex
{
private:
    pthread_mutex_t *_lock_p;

public:
    Mutex(pthread_mutex_t *lock_p) : _lock_p(lock_p)
    {
    }

    void *lock()
    {
        if (_lock_p)
        {
            pthread_mutex_lock(_lock_p);
        }
    }

    void *unlock()
    {
        if (_lock_p)
        {
            pthread_mutex_unlock(_lock_p);
        }
    }
};



class LockGuard // RAII 风格的调用
{
private:
    Mutex _mutex;

public:
    LockGuard(pthread_mutex_t *mutex) : _mutex(mutex)
    {
        _mutex.lock(); // 在构造函数中加锁
    }
    ~LockGuard()
    {
        _mutex.unlock(); // 在析构函数中进行解锁
    }
};